<?php

/**
 * This is the model class for table "{{mem_addres}}".
 *
 * The followings are the available columns in table '{{mem_addres}}':
 * @property integer $zma_id
 * @property integer $zm_id
 * @property string $zma_name
 * @property string $zma_zip
 * @property string $zma_tel
 * @property string $zma_mob
 * @property integer $zar_id
 * @property string $zma_email
 * @property string $zma_address
 * @property integer $zma_default
 */
class MemAddres extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemAddres the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_addres}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zma_default,zma_default_sent', 'numerical', 'integerOnly'=>true),
			array('zma_name', 'length', 'max'=>20),
			array('zma_zip', 'length', 'max'=>6),
			array('zma_tel', 'length', 'max'=>15),
			array('zma_mob', 'length', 'max'=>11),
			array('zar_id', 'numerical', 'message'=>'请选择地址'),
			array('zma_name, zar_id, zma_mob, zma_address', 'required'),				
			array('zma_email, zma_address', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zma_id, zm_id, zma_name, zma_zip, zma_tel, zma_default_sent, zma_mob, zar_id, zma_email, zma_address, zma_default', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'member'=>array(self::HAS_ONE, 'Member', 'zm_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zma_id' => 'id',
			'zm_id' => '会员ID',
			'zma_name' => '收货人姓名',
			'zma_zip' => '邮政编码',
			'zma_tel' => '电话',
			'zma_mob' => '手机',
			'zar_id' => '所在地区',
			'zma_email' => '邮箱',
			'zma_address' => '收货地址',
			'zma_default' => '是否收货默认',
			'zma_default_sent' => '是否发货默认',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zma_id',$this->zma_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zma_name',$this->zma_name,true);
		$criteria->compare('zma_zip',$this->zma_zip,true);
		$criteria->compare('zma_tel',$this->zma_tel,true);
		$criteria->compare('zma_mob',$this->zma_mob,true);
		$criteria->compare('zar_id',$this->zar_id);
		$criteria->compare('zma_email',$this->zma_email,true);
		$criteria->compare('zma_address',$this->zma_address,true);
		$criteria->compare('zma_default',$this->zma_default);
		$criteria->compare('zma_default_sent',$this->zma_default_sent);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 列表
	 * @return CActiveDataProvider
	 */
	public function listSearch()
	{
		$criteria=new CDbCriteria;
		$criteria->order = 'zma_id desc';
		$criteria->compare('zm_id',Yii::app()->user->userid);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	/**
	 *如果勾选了设为默认，把其它都取消
	 */
	protected function beforeSave(){
	    if (parent::beforeSave()){   
	        if($this->zma_default=='1'){
	           $this->updateAll(array('zma_default'=>'0'),'zm_id=:zmid',array(':zmid'=>$this->zm_id));
	        }
	        if($this->zma_default_sent=='1'){
	        	$this->updateAll(array('zma_default_sent'=>'0'),'zm_id=:zmid',array(':zmid'=>$this->zm_id));
	        }
	        return true;
	    }else{
	        return false;
	    }
	}
	
	/** 
	 *取当前商家会员的地址信息
	 */
	public function getCmpAddress($id=null){
		if($id==null){
			$id = Company::model()->getCmpUserId();
		}
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',$id);
		$result = $this->findAll($criteria);
		foreach($result as $val){
			if($val->zma_default==1){
				$address['trx'] = $val;//默认收货地址
			}
			if($val->zma_default_sent==1){
				$address['sent'] = $val;//默认寄货地址
			}
			$address['all'][] = $val;//全部地址
		}
		return $address;
	}
	
	/**
	 * 根据地址ID获取会员详细联系信息
	 */
	public function getMemAddressInfo($id){
		$address = $this->findByPk($id);
		$area = Area::model()->getAreaStrByFloorId($address->zar_id);
		return array(
			'str'=>$address->zma_name.','.$area.$address->zma_address.','.$address->zma_mob.','.$address->zma_tel.','.$address->zma_zip,
			'address'=>$area.$address->zma_address,
			'param'=>$address->attributes,
			);
	}
}